// pages/preview/preview.js
const unrecivedCards = require("../../mock/data/unrecived")[0];
const utils = require("../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    cardId: "",
    showHoldState: false,
    btnShow: false,
    label: "",
    group: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("options", options);
    const userId = wx.getStorageSync("userId");
    this.setData({
      cardId: options.cardId || "",
      detail: options,
      userId
    });
  },

  onShow: function() {
    utils
      .preCheck(true)
      .then(res => {
        this.getState();
      })
      .catch(res => {});
  },
  onShareAppMessage: function(res) {
    return {
      title: this.data.detail.name + "分享了一张名片",
      path: "/pages/index/index?shareCardId=" + this.data.cardId
    };
  },
  getState() {
    utils
      .ajax("/v1/bcs/getBusinessCardWithHoldState.do", {
        cardId: this.data.cardId
      })
      .then(res => {
        if (res.code === "000000") {
          let detail = res.data.cardInfo;
          if (detail.createBy == wx.getStorageSync("userId")) {
            detail.createByMe = true;
          } else {
            detail.createByMe = false;
            this.getLabelAndGroup()
          }
          if (detail.cardId == getApp().globalData["myCards"][0]["cardId"]) {
            detail.isMine = true;
          } else {
            detail.isMine = false;
          }
          this.setData({
            showHoldState: true,
            btnShow: true,
            detail: Object.assign({}, this.data.detail, detail)
          });
        } else {
          console.log(res.message || "获取持有状态信息失败");
        }
      })
      .catch(res => {});
  },
  getLabelAndGroup(){
    utils
      .ajax("/v1/bcs/queryUserCardLables.do", {
        cardId: this.data.cardId
      })
      .then(res => {
        if (res.code === "000000") {
          this.setData({
            label: res.data.labels.length>0?res.data.labels[0]: ""
          })
        } else {
          console.log(res.message || "获取标签失败");
        }
      })
      .catch(res => {});
    utils
      .ajax("/v1/bcs/queryUserCardExistGroups.do", {
        cardId: this.data.cardId
      })
      .then(res => {
        if (res.code === "000000") {
          this.setData({
            group: res.data.groups.length > 0 ? res.data.groups[0].wxGroupId: ""
          })
        } else {
          console.log(res.message || "获取标签失败");
        }
      })
      .catch(res => { });
  },
  editCard() {
    let url =
      "/pages/edit-card/edit-card?" + utils.serializeUrl(this.data.detail);
    wx.navigateTo({
      url
    });
  },
  acceptCardEvent() {
    // 点击接收名片按钮
    this.acceptCard();
  },
  acceptCard(again) {
    // 接收卡片
    utils
      .ajax("/v1/bcs/updateUserCardHolder.do", {
        holdId: this.data.detail.holderId || this.data.detail.holdId,
        state: "1"
      })
      .then(res => {
        if (res.code === "000000") {
          utils.message("已添加到我的名片夹");
          this.setData({
            ["detail.state"]: "1"
          });
        } else {
          utils.message(res.message || "接受失败");
          console.log(res.message || "接受失败");
        }
      })
      .catch(res => {});
  },
  saveToMine() {
    // 点击保存到我的名片夹事件
    wx.showLoading({
      mask: true,
      title: "正在保存"
    });
    this.saveOtherToMine();
  },
  saveOtherToMine() {
    // 保存到我自己的名片夹
    utils
      .ajax("/v1/bcs/addUserCardHolder.do", {
        cardId: this.data.cardId
      })
      .then(res => {
        wx.hideLoading();
        if (res.code === "000000") {
          utils.message("已添加到我的名片夹");
          this.setData({
            ["detail.state"]: "1"
          });
        } else {
          utils.message(res.message || "保存到名片夹失败");
          console.log(res.message || "保存到名片夹失败");
        }
      })
      .catch(res => {});
  },
  deleteCardEvent() {
    console.log(1);
    const _this = this;
    // 点击删除名片的按钮
    wx.showModal({
      title: "提示",
      content: "确定要删除这张名片吗?",
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            mask: true,
            title: "正在删除"
          });
          _this.deleteCard();
        }
      }
    });
  },
  deleteCard() {
    // 从我的名片夹删除
    utils
      .ajax("/v1/bcs/updateUserCardHolder.do", {
        holdId: this.data.detail.holderId || this.data.detail.holdId,
        state: "0"
      })
      .then(res => {
        wx.hideLoading();
        if (res.code === "000000") {
          utils.message("删除成功");
          wx.navigateBack({
            delta: 1
          });
        } else {
          utils.message(res.message || "删除名片失败");
          console.log(res.message || "删除名片失败");
        }
      })
      .catch(res => {});
  },
  deleteMyCard() {
    utils
      .http("/v1/bcs/deleteUserCard.do", {
        cardId: this.data.detail.cardId
      })
      .then(res => {
        if (res.code === "000000") {
          utils.message("删除成功");
          setTimeout(r => {
            wx.navigateBack({
              delta: 1
            });
          }, 1000);
        } else {
          utils.message(res.message);
          console.log(res.message || "删除自己的名片失败");
        }
      })
      .catch(res => {});
  }
});
